package com.scaffold.myproject.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lx
 * @date 2025/6/5
 * @Description
 */
@Configuration // 标明是配置类
@EnableOpenApi   //开启swagger功能
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30) // OpenAPI 3.0
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.scaffold.myproject.web.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
//                .globalRequestParameters(getGlobalRequestParameters());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("MyProject 接口文档")
                .description("基于 Spring Boot 的脚手架项目 API")
                .version("1.0.0")
                .build();
    }


    //生成全局通用参数
    private List<RequestParameter> getGlobalRequestParameters() {
        List<RequestParameter> parameters = new ArrayList<>();
        parameters.add(new RequestParameterBuilder().name("Operating-Company-Id").description("企业id").in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)).defaultValue("111111"))
                .build());
        parameters.add(new RequestParameterBuilder().name("Owned-Company-Id").description("代账企业id").in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)).defaultValue("222222"))
                .build());
        parameters.add(new RequestParameterBuilder().name("x-user-id").description("用户id").in(ParameterType.HEADER)
                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)).defaultValue("333333"))
                .build());
        parameters.add(new RequestParameterBuilder().name("Authorization").description("token").in(ParameterType.HEADER)
                .build());
        return parameters;
    }
}
